﻿using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
namespace UIFrame
{
    /// <summary>
    /// 滑动条
    /// </summary>
    public class SliderItem : CommonItem
    {
        private Slider slider;
        private Text numText;
        private Image Handle;
        public UnityAction BackAction;

        public void RegisgerCommon()
        {
            Handle.color = new Color(0.9333333f, 0.2470588f, 0.3019608f, 1f);
            KeyTab.Instance.RegisterCommon(this);
            KeyTab.Instance.RegisterDownAction(DownEvent);
            KeyTab.Instance.RegisterUpAction(UpEvent);
            KeyTab.Instance.RegisterEnterAction(BackAction);
            KeyTab.Instance.RegisterEscAction(BackAction);
        }
        private void UpEvent()
        {
            SliderValueAdd(1);
        }

        private void DownEvent()
        {
            SliderValueAdd(-1);
        }
        private void Awake()
        {
            slider = transform.Find("Slider").GetComponent<Slider>();
            numText = transform.Find("NumText").GetComponent<Text>();
            Handle = transform.Find("Slider/Handle Slide Area/Handle").GetComponent<Image>();
            BackAction += () =>
            {
                Handle.color = Color.white;           
            };
        }

        public void Start()
        {
            slider.onValueChanged.AddListener((float value) =>
            {
                numText.text = value.ToString();
            });
        }

        public void SliderValueAdd(int num)
        {
            if (slider.value <= 0 && num < 0 || slider.value >= 10 && num > 0)
                return;
            slider.value += num;
        }

        public int GetSliderValue()
        {
            return (int)slider.value;
        }

        public void SetSliderValue(int value)
        {
            slider.value = value;
        }
    }
}

